package com.yzy.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yzy.entity.Collection;
import com.yzy.entity.Goods;
import com.yzy.mapper.CollectionMapper;
import com.yzy.mapper.GoodsMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class CollectionService extends ServiceImpl<CollectionMapper, Collection> {

    @Resource
    private CollectionMapper collectionMapper;

    @Resource
    private GoodsMapper goodsMapper;

    public List<Goods> queryCollecting(Integer userId){
        QueryWrapper<Collection> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",userId);
        List<Collection> list = list(wrapper);

        List<Goods> goodsList = new ArrayList<>();
        List<Integer> idList = new ArrayList<>();
        for(Collection collection : list){
            idList.add(collection.getGoodsId());
        }

        QueryWrapper<Goods> goodsQueryWrapper = new QueryWrapper<>();
        goodsQueryWrapper.in("id",idList);

        return goodsMapper.selectList(goodsQueryWrapper);
    }

    /**
     * @description 判断用户是否收藏了对应商品
     * @param userId 用户id
     * @param goodsId 商品id
     * @return boolean 返回值，已收藏是true，未收藏是false
     * @author mgl
     * @date 2023/8/12 11:59
     */
    public boolean judgeCollecting(Integer userId,Integer goodsId){
        QueryWrapper<Collection> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",userId);
        wrapper.eq("goods_id",goodsId);
        return count(wrapper) > 0;
    }
}





